﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CustomLesson.Controls
{
    /// <summary>
    /// DateTimePicker.xaml 的交互逻辑
    /// </summary>
    public partial class DateTimePicker : UserControl
    {
        public event EventHandler<DateTime> SelectedChanged;

        public DateTime CurrentDateTime
        {
            get { return (DateTime)GetValue(CurrentDateTimeProperty); }
            set { SetValue(CurrentDateTimeProperty, value); }
        }

        public static readonly DependencyProperty CurrentDateTimeProperty =
            DependencyProperty.Register("CurrentDateTime",
                typeof(DateTime),
                typeof(DateTimePicker),
                new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(OnCurrentDateTimeChanged)));

        private static void OnCurrentDateTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as DateTimePicker).Refresh();
        }

        private void Refresh()
        {
            this.calendar.SelectedDate = this.CurrentDateTime;

            this.tb_Hour.Text = this.CurrentDateTime.Hour.ToString();
            this.tb_minute.Text = this.CurrentDateTime.Minute.ToString();
            this.tb_second.Text = this.CurrentDateTime.Second.ToString();
        }

        public DateTimePicker()
        {
            InitializeComponent();
        }

        private void BtnAccept_Click(object sender, RoutedEventArgs e)
        {
            //日期时间合并
            int year = this.calendar.SelectedDate.Value.Year;
            int month = this.calendar.SelectedDate.Value.Month;
            int day = this.calendar.SelectedDate.Value.Day;

            int.TryParse(this.tb_Hour.Text.Trim(), out int hour);
            int.TryParse(this.tb_minute.Text.Trim(), out int minute);
            int.TryParse(this.tb_second.Text.Trim(), out int second);

            CurrentDateTime = new DateTime(year, month, day, hour, minute, second);

            this.popup.IsOpen = false;

            SelectedChanged?.Invoke(this, CurrentDateTime);
        }

        private void BtnHourUp_Click(object sender, RoutedEventArgs e)
        {
            int.TryParse(this.tb_Hour.Text.Trim(), out int hour);

            hour++;
            hour %= 24;

            this.tb_Hour.Text = hour.ToString();
        }

        private void BtnMinuteUp_Click(object sender, RoutedEventArgs e)
        {
            RepeatButton btn = sender as RepeatButton;
            TextBox tb = btn.Tag as TextBox;

            int.TryParse(tb.Text.Trim(), out int value);

            value++;
            value %= 60;

            tb.Text = value.ToString();
        }

        private void BtnHourDown_Click(object sender, RoutedEventArgs e)
        {
            int.TryParse(this.tb_Hour.Text.Trim(), out int hour);

            hour--;
            if (hour < 0) hour = 23;

            this.tb_Hour.Text = hour.ToString();
        }

        private void BtnMinuteDown_Click(object sender, RoutedEventArgs e)
        {
            RepeatButton btn = sender as RepeatButton;
            TextBox tb = btn.Tag as TextBox;

            int.TryParse(tb.Text.Trim(), out int value);

            value--;
            if (value < 0) value = 59;

            tb.Text = value.ToString();
        }

        private void tb_Hour_TextChanged(object sender, TextChangedEventArgs e)
        {
            var tb = sender as TextBox;

            if (!string.IsNullOrEmpty(tb.Text.Trim()))
            {
                int.TryParse(tb.Text.Trim(), out int hour);

                if (hour < 0)
                {
                    hour *= -1;

                    tb.TextChanged -= tb_Hour_TextChanged;
                    tb.Text = tb.Text.Substring(1);
                    tb.TextChanged += tb_Hour_TextChanged;
                }
                if (hour > 23)
                {
                    tb.TextChanged -= tb_Hour_TextChanged;
                    tb.Text = tb.Text.Substring(0, tb.Text.Length - 1);
                    tb.TextChanged += tb_Hour_TextChanged;
                }
            }
        }

        private void tb_minute_TextChanged(object sender, TextChangedEventArgs e)
        {
            var tb = sender as TextBox;

            if (!string.IsNullOrEmpty(tb.Text.Trim()))
            {
                int.TryParse(tb.Text.Trim(), out int minute);

                if (minute < 0)
                {
                    minute *= -1;

                    tb.TextChanged -= tb_Hour_TextChanged;
                    tb.Text = tb.Text.Substring(1);
                    tb.TextChanged += tb_Hour_TextChanged;
                }
                if (minute > 59)
                {
                    tb.TextChanged -= tb_minute_TextChanged;
                    tb.Text = tb.Text.Substring(0, tb.Text.Length - 1);
                    tb.TextChanged += tb_minute_TextChanged;
                }
            }
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.popup.IsOpen = false;
        }
    }
}
